import 'package:flutter/material.dart';
import 'package:flutter_base/page/knowleage/detail/knowledge_detail_page.dart';
import 'package:flutter_base/utils/navigator_utils.dart';
import 'package:flutter_base/viewmodel/knowledge/knowledge_vm.dart';
import 'package:provider/provider.dart';

///
/// @DIR_PATH：lib/page/hot
/// @TIME：2024/6/11 14:43
/// @AUTHOR：starr
/// 热点界面
class KnowledgePage extends StatefulWidget {
  const KnowledgePage({super.key});

  @override
  State<StatefulWidget> createState() => _KnowledgePageState();
}

class _KnowledgePageState extends State<KnowledgePage> with AutomaticKeepAliveClientMixin{
  KnowledgeViewModel viewModel = KnowledgeViewModel();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    viewModel.knowledgeList();
  }

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (context) {
        return viewModel;
      },
      child: Consumer<KnowledgeViewModel>(
        builder: (context, vm, child) {
          return ListView.builder(
            itemCount: vm.entity?.length ?? 0,
            itemBuilder: (BuildContext context, int index) {
              return GestureDetector(
                onTap: (){
                  NavigatorUtils.openPageByFade(context, KnowledgeDetailPage(tabList:vm.entity?[index].children));
                },
                child: Container(
                  decoration: BoxDecoration(
                    border: Border.all(color: Colors.grey),
                    borderRadius: BorderRadius.circular(5),
                  ),
                  padding: EdgeInsets.symmetric(horizontal: 10, vertical: 10),
                  margin: EdgeInsets.symmetric(vertical: 5, horizontal: 10),
                  child: Row(
                    children: [
                      Expanded(
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Text(
                              vm.entity?[index].name ?? "",
                              style: TextStyle(
                                  fontSize: 16, fontWeight: FontWeight.bold),
                            ),
                            SizedBox(
                              height: 5,
                            ),
                            Text(
                              vm.getSubTitle(vm.entity?[index].children) ?? "",
                              style: TextStyle(
                                  fontSize: 14, color: Colors.grey[600]),
                            ),
                          ],
                        ),
                      ),
                      Container(
                        child: const Icon(
                          Icons.arrow_forward_ios,
                          size: 18,
                        ),
                      )
                    ],
                  ),
                ),
              );
            },
          );
        },
      ),
    );
  }

  @override
  bool get wantKeepAlive => true;
}
